let {Vuex} = window; // 外部插件


import slidemenu from './global/slideMenu.vue';

import VueRouter from 'vue-router';
// 
import index from './index/index.vue';
// 
import products     from './product/list.vue';
import products_new from './product/new.vue';
import orders       from './order/list.vue';
// 
import currency  from './currency/list.vue';
// 
import comments       from './comment/list.vue';
import comments_new   from './comment/new.vue';

//
import sites                from './sites/sites.vue'
import sites_setting        from './sites/edit/settings.vue'
import sites_show           from './sites/show.vue'
import sites_search         from './sites/search.vue'
import sites_new_article    from './sites/new/article.vue'
import sites_new_focus      from './sites/new/focus.vue'

// 
import home_setting  from  './home/setting.vue';
import home_setting_edit  from  './home/setting_edit.vue';


// import article     from './article/list.vue'
import article_new from './article/new.vue'

import user           from './user/list.vue'
import user_edit      from './user/edit.vue'
import usergroup      from './usergroup/list.vue'
import usergroup_edit from './usergroup/edit.vue'
import stat           from './stat/stat.vue'
import pay           from './pay/list.vue'
import pay_edit           from './pay/new.vue'

import Service       from './service.js';
import cachePolicy   from './cache.js'
import ElementUI from 'element-ui'; 
//import myDatePicker from './date-picker';


/* Magic:  
 *   element front-end group may think less about webpack'ed Vue than window.Vue, 
 *   add some preloads with correct orders
 */
import Vue from 'vue'
window.Vue = Vue;
Vue.use(ElementUI);


Vue.use(Service, { cachePolicy });
Vue.use(VueRouter);
Vue.use(Vuex);
//Vue.use(myDatePicker);




const store = new Vuex.Store({
    state: {
        username: ''
        , administrator: false
    }
    , mutations: {
        login(state, response){
            state.username = response.username;
            state.administrator = response.is_admin ? true : false ;
        }
    }
})


const routes = [
  { path: '/', component: index }
  , { path: '/products', component: products }
  , { path: '/products/new', component: products_new }
  , { path: '/orders', component: orders }
  , { path: '/currency', component: currency }
  , { path: '/comments', component: comments }
  , { path: '/comments/new', component: comments_new }
  , { path: '/comments/new/:id', component: comments_new }
  , { path: '/sites',   component: sites}
  , { path: '/sites/:domain',   component: sites}
  , { path: '/sites/edit/:domain',   component: sites_setting}  
  , { path: '/sites/show',   component: sites_show}  // debug
  , { path: '/sites/search',   component: sites_search}  // debug
  , { path: '/sites/new/article', component: sites_new_article}
  , { path: '/sites/new/focus',   component: sites_new_focus}

  // , { path: '/article', component: article }
  , { path: '/article/new', component: article_new }
  , { path: '/article/new/:id/:domain', component: article_new }

  , { path: '/home', component: home_setting }
  , { path: '/home/setting', component: home_setting }
  , { path: '/home/setting/edit/:domain', component: home_setting_edit }
  , { path: '/user',      component: user }
  , { path: '/user/edit',      component: user_edit }
  , { path: '/user/edit/:uid',      component: user_edit }
  , { path: '/usergroup', component: usergroup }
  , { path: '/usergroup/edit',           component: usergroup_edit }
  , { path: '/usergroup/edit/:user_group_id',      component: usergroup_edit }
  , { path: '/stat', component: stat},
  , { path: '/pay', component: pay}
  , { path: '/pay/edit',     component: pay_edit}
  , { path: '/pay/edit/:id', component: pay_edit}
]


const router = new VueRouter({ routes });

let tm = null;
let fn = function(){
    tm = new Date();
    let path = window.location.hash;
    path += '$';
    let items = [].slice.call(document.querySelectorAll(".slideMenu li"));
    items.forEach( it => it.classList.remove('is-active'));
    
    items.forEach( it => it.addEventListener('click', e => {
          let a = document.createEvent('MouseEvent'); 
          a.initEvent('click', false, false); 
          it.firstChild.firstChild.click(a);
    }));
    try{
        items.forEach( it => {
          if((it.firstChild.href + '$').indexOf(path)> -1){
              it.classList.add('is-active');
              throw 'break';
          }  
        });
    }catch(e){}
};
window.addEventListener('hashchange', fn);
window.addEventListener('load', fn);






new Vue({
  el: '#app'
  , router
  , store
  , data: {
    message: "Hello Vue"
    , eventHub: (Vue.rootHub = new Vue({ data(){
      return {
        admin: "",
      }
    }})),
  }
  , components: {
    index
    , slidemenu
    , products
    , orders
    , products_new
    , currency
    , comments
    , comments_new
    , home_setting
    , home_setting_edit
    , sites_setting
    // , article
    , article_new
  } 
  , mounted() {

  }
});   
